import utils from './utils'
import constants from './constants'

export default class Constant {
  errorCode = {
    cancel: 900,
    noResponse: 901
  }

  filePath = {
    // 附件上传
    arrUpload: '/system/v1/upload',
    // 附件上传(到公告目录)
    arrUploadPublic: '/root/v1/oss/publicUpload'
  }

  // options是为type为keyValue的选择组件提供选项，key对应OptionsDataSource的value
  options = {}
  constructor () {
    // 将以V开头的属性增加一个k和v反转的对象，并且转为label和value的数组放到options里面
    this.merge(this)
    this.merge(constants)
  }

  addDict = (key, value) => {
    const obj = {}
    value.forEach(v => (obj[v.label] = v.value))
    this[key + 'V'] = obj
    this[key + 'K'] = utils.reverse(obj)
    this.options[key] = value
  }

  addDict4p = (ps, codes) => {
    return Promise.all(ps).then((res) => {
      res.forEach((res, index) => {
        const data = res.data || res
        if (Array.isArray(data)) {
          const key = utils.toHump(codes[index])
          this.addDict(key, data)
        }
      })
      return true
    })
  }

  merge (obj) {
    for (const [key, value] of Object.entries(obj)) {
      if (!this[key]) {
        this[key] = value
      }
      if (key.endsWith('V')) {
        const name = key.substr(0, key.length - 1)
        this[name + 'K'] = utils.reverse(value)
        this.options[name] = utils.objToArr(value)
      }
    }
  }

  // async initDict (store, action) {
  //   // const ps = codes.map(code => store.dispatch('system/getDict', code))
  //   // system/getValueSetListCodes
  //   const { data } = await store.dispatch(action, this.codes)
  //   if (data.result) {
  //     for (const [key, value] of Object.entries(data.result)) {
  //       this.addDict(
  //         utils.toHump(key),
  //         value.map(item => ({ label: item.itemText, value: item.itemValue }))
  //       )
  //     }
  //   }
  //   // return this.addDict4p(ps, codes)
  // }
}
